#ifndef __SERVER_H__
#define __SERVER_H__

#include <soar.h>

__BEGIN_DECLS

/*----- monitor -----*/
#define MONITOR_IDLE_TIMEOUT    SECOND_OF_MINUTE(5) /* 监控闲置超时 */

struct monitor_ctl
{/*{{{*/
    SKT skt;
    char *msg_in;
    char *msg_out;
};/*}}}*/

extern int thrd_monitor(struct monitor_ctl *monitor_ctl);

/*----- listener -----*/
struct listener_ctl
{/*{{{*/
    unsigned long thrd_id;
    EP ep;
    EP_SLOT listener_slot;
    EP_SLOT *worker_slot;
};/*}}}*/

extern void thrd_listener(struct listener_ctl *listener_ctl);

/*----- worker -----*/
#define WORKER_NUM                  8   /* 工作线程数量 */

struct worker_ctl
{/*{{{*/
    unsigned long thrd_id;
    int worker_idx;
    EP ep;
    EP_SLOT listener_slot;
    //char *msg_in;
    char *msg_out;

    time_t last_call;
    unsigned int *svc_count;
    unsigned int *client_count;
};/*}}}*/

extern void thrd_worker(struct worker_ctl *worker_ctl);

typedef int (*P_MONSVC_FUNC)(char *, char *);
#define MONSVC_FUNC(_svc)   \
int MONSVC_FUNC_##_svc(char *body, char *msg_out)

typedef int (*P_SVC_FUNC)(int, char *, char *);
#define SVC_FUNC(_svc)   \
int SVC_FUNC_##_svc(int worker_idx, char *body, char *msg_out)

/*----- 共享信息 -----*/
struct _cnfg_info
{/*{{{*/
    char server[NAME_SIZE+1];
    char user[NAME_SIZE+1];
    struct
    {
        char location[LONG_FILE_NM_SIZE+1];
        unsigned int tracking;
    } _errlog_;
    struct
    {
        unsigned int server_port;
        unsigned int monitor_port;
    } _access_;
    struct
    {
        time_t access_timeout;
    } _session_;
};/*}}}*/
extern struct _cnfg_info cnfg_info;

struct _share_info
{/*{{{*/
    pthread_mutex_t svc_info_lock;
    struct _svc_info
    {
        char *name;
        unsigned int loop;
        unsigned int count;
    } *svc_info;
    int svc_num;

    pthread_mutex_t worker_info_lock;
    struct _worker_info
    {
        struct _client_info
        {
            char *ip;
            unsigned int port;
            unsigned int loop;
            unsigned int count;
        } client_info[EP_POLL_MAX_NUM];
        int client_num;
    } worker_info[WORKER_NUM];
    int *hash_users;
    int hash_num;
};/*}}}*/
extern struct _share_info share_info;

__END_DECLS

#endif
